package com.zj.eswrapper.wrapper;

import com.zj.eswrapper.interfaces.field.IStringField;
import com.zj.eswrapper.interfaces.impl.AbstractEsSearchWrapper;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author arthur_zhou
 * @date 2023/12/28 18:37
 */
public class EsQueryWrapper<DO> extends AbstractEsLastQueryWrapper<String, EsQueryWrapper<DO>, EsSearchWrapper<DO>, EsAggWrapper<DO>>
        implements IStringField<EsLambdaQueryWrapper<DO>> {

    public EsQueryWrapper() {
        this(true);
    }

    /**
     * @param check 是否校验val的值。true时，val不能为空。关联的EsQueryWrapper全局公用
     */
    public EsQueryWrapper(boolean check) {
        this(new AtomicBoolean(check));
    }

    protected EsQueryWrapper(AtomicBoolean check) {
        super(check);
    }

    protected EsQueryWrapper(AbstractEsLastQueryWrapper<?, ?, ?, ?> queryWrapper) {
        super(queryWrapper);
    }

    @Override
    protected EsQueryWrapper<DO> createWrapper() {
        return new EsQueryWrapper<>(this.check);
    }

    @Override
    protected EsAggWrapper<DO> createAggWrapper() {
        return new EsAggWrapper<>();
    }

    @Override
    protected EsSearchWrapper<DO> createSearchWrapper(AbstractEsSearchWrapper<?, ?, SearchSourceBuilder> searchWrapper) {
        if (Objects.nonNull(searchWrapper)) {
            return new EsSearchWrapper<>(searchWrapper);
        }
        return new EsSearchWrapper<>(searchBuilder);
    }

    /**
     * 转换成lambda条件拼接器
     *
     * @return EsLambdaQueryWrapper
     */
    @Override
    public EsLambdaQueryWrapper<DO> lambda() {
        return new EsLambdaQueryWrapper<>(this);
    }
}
